{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "# **Helicone - LLM Observability Demo**"
      ],
      "metadata": {
        "id": "YxzY3N_z73et"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "Install the necessary packages"
      ],
      "metadata": {
        "id": "cYCHssNC8KS7"
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "BEZqBDGz71qX"
      },
      "outputs": [],
      "source": [
        "!pip install streamlit\n",
        "!pip install langchain\n",
        "!pip install openai\n",
        "!pip install helicone"
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "Create a Streamlit app and save to a local file"
      ],
      "metadata": {
        "id": "GwsXkJ7T8P8T"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "%%writefile app.py\n",
        "import openai, streamlit as st\n",
        "from langchain.llms.openai import OpenAI\n",
        "\n",
        "openai.api_base = \"https://oai.hconeai.com/v1\"\n",
        "\n",
        "# Streamlit app\n",
        "st.title('LLM Observability Demo')\n",
        "\n",
        "# Get OpenAI API key and user query\n",
        "openai_api_key = st.text_input(\"OpenAI API Key\", type=\"password\")\n",
        "helicone_api_key = st.text_input(\"Helicone API Key\", type=\"password\")\n",
        "user_query = st.text_input(\"Your Query\")\n",
        "\n",
        "# If the 'Submit' button is clicked\n",
        "if st.button(\"Submit\"):\n",
        "    # Validate inputs\n",
        "    if not openai_api_key.strip() or not helicone_api_key.strip() or not user_query.strip():\n",
        "        st.error(f\"Please provide the missing fields.\")\n",
        "    else:\n",
        "        try:\n",
        "            llm = OpenAI(\n",
        "              temperature=0.9, \n",
        "              openai_api_key=openai_api_key, \n",
        "              headers={\n",
        "                \"Helicone-Auth\": f\"Bearer {helicone_api_key}\",\n",
        "                \"Helicone-Cache-Enabled\": \"true\"\n",
        "              }\n",
        "            )\n",
        "            \n",
        "            st.success(llm(user_query))\n",
        "        except Exception as e:\n",
        "            st.error(f\"An error occurred: {e}\")"
      ],
      "metadata": {
        "id": "tMmaFr_N8Q37"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Install localtunnel to serve the Streamlit app"
      ],
      "metadata": {
        "id": "6BKDchAE8aLo"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "!npm install localtunnel"
      ],
      "metadata": {
        "id": "uVcyV0PU8a18"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Run the Streamlit app in the background"
      ],
      "metadata": {
        "id": "z-NdVJF98dEH"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "!streamlit run app.py &>/content/logs.txt &"
      ],
      "metadata": {
        "id": "VYOU5fhn8f-m"
      },
      "execution_count": 4,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Expose the Streamlit app on port 8501"
      ],
      "metadata": {
        "id": "-ibaCA9f8jBg"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "!npx localtunnel --port 8501"
      ],
      "metadata": {
        "id": "dG2gbs5k8ls_"
      },
      "execution_count": null,
      "outputs": []
    }
  ]
}